﻿using SalarySimulationPayment.Entitys;
using SqlSugar;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;

namespace SalarySimulationPayment
{
    public partial class SalaryMain : UIForm
    {
        SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
        {
            ConnectionString = @"Data Source=.\SalarySimulationPayment.db;Version=3;",
            DbType = SqlSugar.DbType.Sqlite,
            IsAutoCloseConnection = true
        });

        public SalaryMain()
        {
            InitializeComponent();
            var list = db.Queryable<SalaryDetail>().ToDataTable();
            salaryDetailView.DataSource = list;
        }

        private void grantBtn_Click(object sender, EventArgs e)
        {
            int selectNum = 0;//发放人数
            int selectTotalNum = 0;//完整工资人数
            double portionNum = 0;//工资发放比例份数
            double totalSalaryNum = ConvertNum(totalSalaryText.DoubleValue);//总工资数
            totalSalaryText.DoubleValue = totalSalaryNum;//自动纠正输入有误的总工资数
            double surplusTotalSalaryNum = totalSalaryNum;//补贴后剩余工资总数
            double rationTotalNum = 0;//黑本工资总比例
            try
            {
                for (int i = 0; i < salaryDetailView.RowCount; i++)//计算参与工资发放的人数
                {
                    if (salaryDetailView[1, i].Value.ToString() == "1")
                    {
                        selectNum++;
                        if (salaryDetailView[5, i].Value.ToString() != "0")
                        {
                            rationTotalNum += ConvertNum(Convert.ToDouble(salaryDetailView[5, i].Value));

                            if (rationTotalNum > 100)
                            {
                                WarningTip("黑本比例不能大于100%！");
                                return;
                            }
                            surplusTotalSalaryNum = surplusTotalSalaryNum - (totalSalaryNum * Convert.ToDouble(salaryDetailView[5, i].Value) / 100);
                        }
                        if (salaryDetailView[6, i].Value.ToString() != "0")
                        {
                            if (Convert.ToDouble(salaryDetailView[6, i].Value) > surplusTotalSalaryNum)
                            {
                                WarningTip("补贴不能大于去掉黑本补贴之后的总工资！");
                                return;
                            }
                            surplusTotalSalaryNum = surplusTotalSalaryNum - ConvertNum(Convert.ToDouble(salaryDetailView[6, i].Value));
                        }
                    }

                }
                for (int i = 0; i < salaryDetailView.RowCount; i++)//计算工资发放份数
                {
                    if (salaryDetailView[4, i].Value.ToString() != "100")
                    {
                        selectTotalNum++;
                        portionNum += 100 * ConvertNum(Convert.ToDouble(salaryDetailView[5, i].Value));
                    }

                }


                for (int i = 0; i < salaryDetailView.RowCount; i++)//发放工资
                {
                    double averageSalary = surplusTotalSalaryNum * 100 / ((selectNum - selectTotalNum) * 100 + portionNum);
                    double grantNum = averageSalary;
                    if (salaryDetailView[1, i].Value.ToString() == "1")
                    {
                        if (salaryDetailView[4, i].Value.ToString() != "100")
                        {
                            averageSalary = Convert.ToDouble(averageSalary * Convert.ToDouble(salaryDetailView[4, i].Value) / 100);
                            grantNum = averageSalary;
                        }
                        if (salaryDetailView[5, i].Value.ToString() != "0")
                        {
                            grantNum = grantNum + totalSalaryNum * ConvertNum(Convert.ToDouble(salaryDetailView[5, i].Value)) / 100;
                        }
                        if (salaryDetailView[6, i].Value.ToString() != "0")
                        {
                            grantNum = grantNum + ConvertNum(Convert.ToDouble(salaryDetailView[6, i].Value));
                        }
                        salaryDetailView[3, i].Value = ConvertNum(averageSalary);
                        salaryDetailView[7, i].Value = ConvertNum(grantNum);
                    }
                    else
                    {
                        salaryDetailView[3, i].Value = Convert.ToDouble(0);
                        salaryDetailView[7, i].Value = Convert.ToDouble(0);
                    }
                }
                remarkText.ForeColor = Color.Black;
                remarkText.Text = "发放成功！恭喜恭喜~";
            }
            catch (Exception ex)
            {
                var list = db.Queryable<SalaryDetail>().ToDataTable();
                salaryDetailView.DataSource = list;
                WarningTip("输入有误,详细原因：" + ex.Message);
            }

        }
        private void WarningTip(string warningMsg)
        {
            remarkText.ForeColor = Color.Red;
            remarkText.Text = warningMsg;
        }
        private double ConvertNum(double oldNum)
        {
            double newNum = Convert.ToDouble(oldNum.ToString("N"));
            return newNum;
        }

        private void openSourceBtn_Click(object sender, EventArgs e)
        {
            string msg = "如果大家有码云（gitee）帐号，请帮忙点个star，感谢！\n";
            msg += "码云（gitee）地址：https://gitee.com/yinchen001/SalarySimulationPayment\n";
            msg += "\n--by 音尘";
            ShowInfoDialog("提示", msg, UIStyle.LightBlue, false);
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {

            DataTable dbSalaryDetail = salaryDetailView.DataSource as DataTable;
            List<SalaryDetail> salaryDetails = Common.Utilities.DataTableExtensions.ToEntities<SalaryDetail>(dbSalaryDetail);

            var res = db.Updateable(salaryDetails).ExecuteCommand();
            if (res <= 0)
            {
                remarkText.ForeColor = Color.Red;
                remarkText.Text = "保存失败";
                return;
            }
            remarkText.ForeColor = Color.Black;
            remarkText.Text = "保存成功";
        }

    }

}
